package com.sk.bilibili.view.fragment.main.collect;

import android.widget.RadioGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.sk.bilibili.R;
import com.sk.bilibili.adapter.vlayout.main.colloct.FollowThemAdapter;
import com.sk.bilibili.bean.FollowThem;
import com.sk.bilibili.callBack.OnBottomCallBack;
import com.sk.bilibili.callBack.OnScrollBottomListener;
import com.sk.bilibili.databinding.FragmentFollowThemBinding;
import com.sk.bilibili.model.fragment.main.collect.FollowThemModel;
import com.sk.bilibili.view.activity.ThemActivity;
import com.sk.bilibili.view.diy.bottomDialog.FollowBottomDialog;
import com.sk.bilibili.view.fragment.BaseFragment;

import java.util.List;

public class FollowThemFragment extends BaseFragment<FragmentFollowThemBinding, FollowThemModel> implements
        RadioGroup.OnCheckedChangeListener, SwipeRefreshLayout.OnRefreshListener, OnBottomCallBack {

    private Integer typeId;
    private FollowBottomDialog followBottomDialog;

    public FollowThemFragment(Integer typeId) {
        this.typeId = typeId;
        followBottomDialog=new FollowBottomDialog();
    }

    @Override
    protected void afterSetView() {
        initView();
        bindEvent();
        model.refresh(typeId);
    }

    private FollowThemAdapter followThemAdapter;

    private void initView() {
        bind.srlShow.setColorSchemeResources(R.color.pink);
        bind.rvShow.setLayoutManager(new LinearLayoutManager(getContext()));
        followThemAdapter = new FollowThemAdapter(getContext(), this);
        bind.rvShow.setAdapter(followThemAdapter);
    }

    private void bindEvent() {
        bind.rgShow.setOnCheckedChangeListener(this);
        bind.srlShow.setOnRefreshListener(this);
        bind.rvShow.addOnScrollListener(new OnScrollBottomListener(this));
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.rb_want:model.setState(0,typeId);break;
            case R.id.rb_look:model.setState(1, typeId);break;
            case R.id.rb_over:model.setState(2, typeId);break;
        }
    }

    @Override
    public void onRefresh() {
        model.refresh(typeId);
    }

    @Override
    public void onBottom() {
        model.loadData(typeId);
    }

    public void refreshSuccess(List<FollowThem> records) {
        followThemAdapter.setData(records);
        bind.srlShow.setRefreshing(false);
    }

    public void loadMoreSuccess(List<FollowThem> records) {
        if(records.isEmpty()){
            toast("没有更多了");
        }else {
            followThemAdapter.addData(records);
        }
    }

    public void onThemClick(Integer id) {
        ThemActivity.start(getContext(),id);
    }

    public void onMoreClick(Integer id) {
        followBottomDialog.show(getChildFragmentManager(),id);
    }

}